#include <stdio.h>
#include <stdlib.h>
#include "sqlite3.h"
#include "sqlFunc.h"
#include <windows.h>
#include <time.h> 
#include <sys/stat.h>
#include <conio.h> 
#include <string.h>
#include "gbkAndUtf.h"
#include "textcodec.h"
//void myfilerecursive(sqlite3 *db,char *path);

char fileType[20]={0};  

//SELECT * FROM cehi WHERE JiaoYanMa LIKE '%131%';ģ����ѯ 

char slqSelectBuf[5000]={0}; 
char slqSelectBuf_2[5000]={0}; 
char slqSelectLen =0;
char sqlitePath[5000]={0}; 
char git_commitTOsqlite_ini[5000]={0};
char *git_commitTOsqlite_iniP="./git_commitTOsqlite.ini",*sqlinitP;

void textTihuan(char * path,char sr,char dc,int len)
{
	int i=0;
	for(i=0;i<len;i++){
		if(path[i]==sr)path[i]=dc;
	}
}

int main(int argc,char **argv)
{
	int i=0,len=0;
	unsigned char pathTextbuf[10000]={0}; 
	unsigned char committextbuf[10000]={0}; 	
	unsigned char committextbuf2[10000]={0}; 
	unsigned long long cmdNum = 0;
	//system("start explorer ./");
	FILE * fp1 = fopen(git_commitTOsqlite_iniP, "r");//�������ļ�
	sqlite3 *db=NULL;
	time_t now ;
    struct tm *tm_now ;
    time(&now) ;
    tm_now = localtime(&now) ;
    sqlinitP = _pgmptr;//argv[0];	//��ȡ�������еľ���·�� 
   // printf("argv[0]:%s\n",argv[0]);   
	if(0==fgets(sqlitePath,sizeof(sqlitePath),fp1)){//�������ļ���ȡһ���ַ���
		int j=0;
		fclose(fp1);

		for(i = 0; sqlinitP[i] != '\0'; ++i);
		for( ;i;--i){
			if(sqlinitP[i]=='/' || sqlinitP[i]=='\\')break;
		}
		for(j=0; j<=i;j++){
			git_commitTOsqlite_ini[j]=sqlinitP[j];
		}
		for(i=0; git_commitTOsqlite_iniP[i];i++){
			git_commitTOsqlite_ini[j+i]=git_commitTOsqlite_iniP[i];
		}
		//printf("argv[0]:%s\n%s\n",argv[0],git_commitTOsqlite_ini);
		fp1 = 0;
		fp1 = fopen(git_commitTOsqlite_ini, "r");//�������ļ�
		if(0==fgets(sqlitePath,sizeof(sqlitePath),fp1)){
			printf("ERR:git_commitTOsqlite.ini Not sqlit path \n %s",sqlitePath);
			fclose(fp1);
			getch();
			return 0;			
		}else{
			//textTihuan(sqlitePath,'\\','/',strlen(sqlitePath));
			//printf("Done:git_commitTOsqlite.ini open sqlit path \n%s",sqlitePath);	
		}

	}else{
			//textTihuan(sqlitePath,'\\','/',strlen(sqlitePath));
			//printf("Done1:git_commitTOsqlite.ini open sqlit path \n%s",sqlitePath);	
	}
	textTihuan(sqlitePath,'\n',0,strlen(sqlitePath));
	textTihuan(sqlitePath,'\r',0,strlen(sqlitePath));
	fclose(fp1);
	
	//printf("argc:%d\n",argc);
#if 0
//	sprintf(pathTextbuf,"%s ",argv[0]);
	//for(i=1;i<argc;i++){
	//	len += sprintf(committextbuf+len,"%s ",argv[i]);
	//}
	//printf("path:%s  commitText:%s\n",pathTextbuf,committextbuf);
#else
	// for(i=1;i<argc;i++){
	// 	len += sprintf(committextbuf+len,"%s ",argv[i]);
	// }
	// printf("committextbuf %d:%s\n",argc,committextbuf);
	//char *found = strstr(str, "world");
	
	len += sprintf(committextbuf,"'%s',",argv[1]);
	for(i=2;i<argc;i++){
		len += sprintf(committextbuf+len,"'%s'",argv[i]);
	}
	
#endif
	if(argc>=2){	//ͨ��git commit -m���ݲ��� 
		echoInsertSql(db,committextbuf,sqlitePath,now); 
		printf("now datetime: %d  %d-%d-%d %d:%d:%d\n",
			now,tm_now->tm_year+1900, tm_now->tm_mon+1, tm_now->tm_mday, tm_now->tm_hour, tm_now->tm_min, tm_now->tm_sec) ;
	}
	while(argc<2){
		memset(slqSelectBuf,0,sizeof(slqSelectBuf));
		memset(strCmd,0,sizeof(strCmd));
		printf("input command or open path or exit end:");
		slqSelectLen = ceshigetchar(slqSelectBuf,sizeof(slqSelectBuf)-1);
		if(getCommcmd(strCmd,slqSelectBuf,5)){
			//printf("strCmd:%s\n",strCmd) ;
			if(strcmp(strCmd,"exit")==0)return 0;
			if(strcmp(strCmd,"open ")==0){
				cmdNum = 0;
				getNumber(&cmdNum,slqSelectBuf,5,50);
				//printf("getNumber:%d\n",cmdNum);
				CmdSelectSql(db,slqSelectBuf_2,sqlitePath,cmdNum);
				continue;	
			}
			
		}
		//printf("strCmd:%s--%s  %d\n",strCmd,"exit",strcmp(strCmd,"exit")) ;
		memset(slqSelectBuf_2,0,sizeof(slqSelectBuf_2));
		echoSelectSql(db,slqSelectBuf,sqlitePath,slqSelectBuf_2);
	}
	
 
}
